package com.yycx.module.bus.provider.handler.haiying.salemanbalance;

import com.yycx.bpm.provider.common.FlowableHelp;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.mapper.BaseUserDeptMapper;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.provider.service.hy.HyOrderDetailService;
import com.yycx.module.file.provider.service.SysFileService;
import com.yycx.module.system.client.entity.SysBank;
import com.yycx.module.system.provider.service.SysBankService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 资保金收款列表
 *
 * @author zyf
 */
@Component
public class SaleBalanceTab1PageListHandler implements PageInterceptor<BusSalemanBalance> {

    @Resource
    private FlowableHelp flowableHelp;

    @Resource
    private SysBankService bankService;

    @Resource
    private SysFileService fileService;

    @Resource
    private HyOrderDetailService hyOrderDetailService;


    @Override
    public void prepare(CriteriaQuery<BusSalemanBalance> cq, PageParams pageParams, EntityMap params) {
        OpenUser openUser = OpenHelper.getUser();
        Long companyId = params.getLong("companyId", openUser.getCompanyId());
        cq.select(BusSalemanBalance.class, "*");
        cq.select(BusDept.class, "deptName");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        cq.addSelect(BusPayment.class, "paymentState","processInstanceId","paymentNo");
        cq.addSelect(BusSaleman.class, "saleManName");
        cq.addSelect(BaseUser.class, "nickName");
        cq.eq(true, "optType", CommonConstants.INT_1);
        cq.eq(true, "bsb.companyId", companyId);
        cq.ge(FlymeUtils.isNotEmpty(beginDate), "amountDate", beginDate);
        cq.le(FlymeUtils.isNotEmpty(endDate), "amountDate", endDate);
        cq.createJoin(BusSaleman.class);
        cq.createJoin(BaseUser.class);
        cq.createJoin(BusDept.class);
        cq.createJoin(BusPayment.class).setMainField("rkId").setJoinField("paymentId");
        cq.orderByDesc("bsb.createTime");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusSalemanBalance> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("processInstanceId");
            Long bankId = entityMap.getLong("bankId");
            Long saleManBalanceId = entityMap.getLong("saleManBalanceId");
            Long saleManId = entityMap.getLong("saleManId");
            SysBank bank=bankService.getById(bankId);
            entityMap.put("bankName",bank.getBankName());
            EntityMap taskInfo = flowableHelp.getTaskInfo(processInstanceId);
            entityMap.putAll(taskInfo);
            //已用金额(已发货未回款的订单占用的额度)
            BigDecimal usedAmount = hyOrderDetailService.totalCustomBondAmount(saleManId);
            BigDecimal amount=entityMap.getBigDecimal("amount");
            entityMap.put("surplusAmount",amount.subtract(usedAmount));
            if (FlymeUtils.isNotEmpty(saleManBalanceId)) {
                List<EntityMap> fileList = fileService.selectFileListByFileType(saleManBalanceId.toString(), "BusSalemanBalance", "");
                entityMap.put("fileList", fileList);
            }
        }
        return result;
    }
}
